package ui;

import Utils.WriteDownUsers;
import domain.User;
import jdk.jshell.execution.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/*
 * 忘记密码界面(拓展)
 * 负责人:bijiajun
 *页面美化:bijiajun
 * */
public class ForgetPasswordJFrame extends JFrame implements ActionListener {
    //确认按钮
    JButton sure1 = new JButton("确定");
    JButton sure2 = new JButton("确定");
    //返回登录按钮
    JButton goBackLogin = new JButton("返回登录");
    //用户名文本框
    JTextField username = new JTextField();
    //邮箱文本框
    JTextField postbox = new JTextField();
    //密码文本框
    JPasswordField password1 = new JPasswordField();
    //确认密码文本框
    JPasswordField password2 = new JPasswordField();
    JLabel passwordText1 = new JLabel("请输入密码");
    JLabel passwordText2 = new JLabel("请再次输入密码");
    JLabel usernameText = new JLabel("请输入账号");
    JLabel postboxText = new JLabel("请输入邮箱");
    StringBuilder usernameInputToJDialog = new StringBuilder();
    StringBuilder postboxInputToJDialog = new StringBuilder();
    //创建一个弹框对象
    JDialog passwordJDialog = new JDialog();
    //定义一个变量来指向通过验证的账户在集合中的下标
    int bjj_index = -1;

    public ForgetPasswordJFrame() {
        //初始化界面
        initJFrame();
        //在这个界面中添加内容
        initView();
        //添加图片
        initImage();
        //是否可见(写在最后)
        this.setVisible(true);
    }
    private void initImage() {
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon(this.getClass().getResource("/image/RegisterJFrame/背景.png")));
        background.setBounds(0, 0, 603, 680);
        this.getContentPane().add(background);
    }

    public void passwordJDialog() {
        initpasswordJDialog();
        initpasswordView();
        initpasswordImage();
        passwordJDialog.setVisible(true);
    }

    private void initpasswordImage() {
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon(this.getClass().getResource("/image/ForgetPasswordJFrame/密码弹窗背景.png")));
        background.setBounds(0, 0, 403, 480);
        passwordJDialog.getContentPane().add(background);
    }

    private void initView() {
        //添加请输入账号文字

        usernameText.setBounds(115, 195, 470, 17);
        usernameText.setFont(new Font("微软雅黑", Font.BOLD, 13));
        this.getContentPane().add(usernameText);
        //添加请输入账号输入框
        username.setBounds(195, 190, 200, 30);
        this.getContentPane().add(username);


        //添加请输入邮箱文字

        postboxText.setBounds(100, 255, 320, 16);
        postboxText.setFont(new Font("微软雅黑", Font.BOLD, 13));
        this.getContentPane().add(postboxText);
        //添加请输入邮箱输入框
        postbox.setBounds(195, 250, 200, 30);
        this.getContentPane().add(postbox);

        //添加确定1按钮
        sure1.setBounds(410, 370, 128, 47);
        sure1.setBackground(new Color(253, 222, 122));
        sure1.addActionListener(this);
        this.getContentPane().add(sure1);


        //添加返回登录按钮
        goBackLogin.setBounds(50, 370, 128, 47);
        goBackLogin.setBackground(new Color(253, 222, 122));
        goBackLogin.addActionListener(this);
        this.getContentPane().add(goBackLogin);


    }

    private void initJFrame() {
        //设置长宽(像素)
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("人生重开模拟器 v1.0   忘记密码界面");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //取消默认的居中方式,只有取消了才会按照,x y轴的方式添加组件
        this.setLayout(null);
        //给整个界面添加键盘监听
        //this.addKeyListener(this);
    }

    //输入密码弹窗
    public void initpasswordJDialog() {

        //给弹框设置大小
        passwordJDialog.setSize(403, 480);
        //让弹框置顶
        passwordJDialog.setAlwaysOnTop(true);
        //让弹框居中
        passwordJDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        passwordJDialog.setModal(true);

        passwordJDialog.setLayout(null);
    }

    public void initpasswordView() {
        //添加请输入密码文字
        passwordText1.setBounds(35, 165, 320, 16);
        passwordText1.setFont(new Font("微软雅黑", Font.BOLD, 13));
        passwordJDialog.getContentPane().add(passwordText1);
        //添加请输入密码的输入框
        password1.setBounds(125, 160, 200, 30);
        passwordJDialog.getContentPane().add(password1);

        //添加请再次输入密码文字
        passwordText2.setBounds(30, 225, 320, 16);
        passwordText2.setFont(new Font("微软雅黑", Font.BOLD, 13));
        passwordJDialog.getContentPane().add(passwordText2);

        //添加请再次输入密码的输入框
        password2.setBounds(125, 220, 200, 30);
        passwordJDialog.getContentPane().add(password2);

        //添加确定2按钮
        sure2.setBounds(225, 300, 128, 47);
        sure2.setBackground(new Color(253, 222, 122));
        sure2.addActionListener(this);
        passwordJDialog.getContentPane().add(sure2);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sure1) {
            Utils.Music.playSound("src\\music\\02.wav");
            //清除
            usernameInputToJDialog.delete(0, usernameInputToJDialog.length());
            postboxInputToJDialog.delete(0, postboxInputToJDialog.length());
            //获取两个个文本输入框中的内容
            String usernameInput = username.getText();
            String postboxInput = postbox.getText();
            if (usernameInput.length() == 0 || postboxInput.length() == 0) {
                System.out.println("账号或邮箱为空");
                //调用showJDialog方法并展示弹框
                Utils.JDialog.showJDialog("账号或邮箱为空");
                return;
            }
            //遍历users确认账号存在或不存在
            boolean found = false; // 新增标志位
            for (int i = 0; i < LoginJFrame.users.size(); i++) {
                if (usernameInput.equals(LoginJFrame.users.get(i).getUsername())
                        && postboxInput.equals(LoginJFrame.users.get(i).getPostbox())) {
                    // 处理匹配成功的逻辑
                    usernameInputToJDialog.append(usernameInput);
                    postboxInputToJDialog.append(postboxInput);
                    bjj_index = i;
                    passwordJDialog();
                    found = true;
                    break;
                }
            }
            if (!found) {
                // 仅在未找到时提示
                System.out.println("账号不存在");
                Utils.JDialog.showJDialog("账号不存在");
            }
        } else if (e.getSource() == sure2) {
            Utils.Music.playSound("src\\music\\02.wav");
            //获取两个密码输入框中的内容
            String password1Input = password1.getText();
            String password2Input = password2.getText();
            if (password1Input.equals(password2Input)) {
                LoginJFrame.users.remove(LoginJFrame.users.get(bjj_index));
                User userInfo = new User(usernameInputToJDialog.toString(), password1Input, postboxInputToJDialog.toString());
                LoginJFrame.users.add(userInfo);
                try {
                    WriteDownUsers.writeDownUsers(LoginJFrame.users);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                //重置密码成功
                System.out.println("重置密码成功");
                for (int i = 0; i < LoginJFrame.users.size(); i++) {
                    System.out.println(LoginJFrame.users.get(i).getUsername() + " " + LoginJFrame.users.get(i).getPostbox() + " " + LoginJFrame.users.get(i).getPassword());
                }

                //关闭当前界面
                passwordJDialog.setVisible(false);
                //调用showJDialog方法并展示弹框
                Utils.JDialog.showJDialog("重置密码成功");
                System.out.println("可以开始进行登录了");
                //打开游戏的登录界面
                new LoginJFrame();
            } else {
                //校验密码是否一致
                System.out.println("两次密码不一致");
                //调用showJDialog方法并展示弹框
                Utils.JDialog.showJDialog("两次密码不一致");
            }

        } else if (e.getSource() == goBackLogin) {
           Utils.Music.playSound("src\\music\\02.wav");
            this.setVisible(false);
            new LoginJFrame();
        }
    }
}
